package com.netrust.quarz.service;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


import com.google.common.collect.Lists;
import com.netrust.quarz.common.IConstant;
import com.netrust.quarz.common.NetrustException;
import com.netrust.quarz.common.enums.OperatorEnums;
import com.netrust.quarz.dao.DateReadRecordDao;
import com.netrust.quarz.dao.HistoryAlarmDao;
import com.netrust.quarz.dao.ServerDao;
import com.netrust.quarz.entity.DateReadRecord;
import com.netrust.quarz.entity.Server;
import com.netrust.quarz.hw.CommonHttp;
import com.netrust.quarz.hw.token.OcUtils;
import com.netrust.quarz.task.utils.AlarmConverUtils;
import com.netrust.quarz.utils.ProdUtils;

/**
 * 生成历史告警服务
 * @author visitor
 */
@Component  
public class BulidAlarmHistoryService {
	
	private static final Logger log = LoggerFactory.getLogger(BulidAlarmHistoryService.class);

	@Autowired
	OcUtils oc;
	@Autowired
	ServerDao serverDao;
	@Autowired
	HistoryAlarmDao historyAlarmDao;
	@Autowired
	DateReadRecordDao dateReadRecordDao;
	
	
	@SuppressWarnings("rawtypes")
	public List http_run(String op) throws Exception{
		String key=ProdUtils.op2Ckey(op);
		String oe=ProdUtils.op2oe(op).getName();
		
		DateReadRecord drd=dateReadRecordDao.findByKey(key);
		
		
		CommonHttp ch=new CommonHttp();
		oc.setMo(oe);//设置读取运营商
		int int_drd_dx_zwww=StringUtils.isNotBlank(drd.getValue())?Integer.parseInt(drd.getValue()):0;//取出上次读取的行号标识位
		List list=ProdUtils.pushList(ch.getAlarmHistory(oc,int_drd_dx_zwww));//获取封装好的已处理告警
		
		return list;
	}
	
	@SuppressWarnings("rawtypes")
	public List<String> http_run_str(String op) throws Exception{
		String key=ProdUtils.op2Ckey(op);
		
		String oe=ProdUtils.op2oe(op).getName();
		
		DateReadRecord drd=dateReadRecordDao.findByKey(key);
		
		CommonHttp ch=new CommonHttp();
		oc.setMo(oe);//设置读取运营商
		int int_drd_dx_zwww=StringUtils.isNotBlank(drd.getValue())?Integer.parseInt(drd.getValue()):0;//取出上次读取的行号标识位
		return ch.getAlarmHistory(oc,int_drd_dx_zwww);//获取封装好的已处理告警
	}
	
	
	@Transactional(isolation=Isolation.DEFAULT,propagation = Propagation.REQUIRED)
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void run() throws Exception{
		NetrustException exception=null;
		
		DateReadRecord drd_dx_zwww=dateReadRecordDao.findByKey(IConstant.drd_dx_zwww);
		DateReadRecord drd_dx_hlw=dateReadRecordDao.findByKey(IConstant.drd_dx_hlw);
		DateReadRecord drd_yd_zwww=dateReadRecordDao.findByKey(IConstant.drd_yd_zwww);
		DateReadRecord drd_yd_hlw=dateReadRecordDao.findByKey(IConstant.drd_yd_hlw);
		
		
		List list=Lists.newArrayList();
		
		try {
			list=this.getAlarmHistory(OperatorEnums.Operator_dx_zwww, drd_dx_zwww, list);
		} catch (Exception e) {
			log.error(OperatorEnums.Operator_dx_zwww.getName()+"获取数据异常>>>>> {}", ProdUtils.exception(e));
			exception=new NetrustException("电信政务外网历史告警数据获取异常", e);
		}

		try {
			list=this.getAlarmHistory(OperatorEnums.Operator_dx_hlw, drd_dx_hlw, list);
		} catch (Exception e) {
			log.error(OperatorEnums.Operator_dx_hlw.getName()+"获取数据异常>>>>> {}", ProdUtils.exception(e));
			exception=new NetrustException("电信互联网历史告警数据获取异常", e);
		}
		
		try {
			list=this.getAlarmHistory(OperatorEnums.Operator_yd_zwww, drd_yd_zwww, list);
		} catch (Exception e) {
			log.error(OperatorEnums.Operator_yd_zwww.getName()+"获取数据异常>>>>> {}", ProdUtils.exception(e));
			exception=new NetrustException("移动政务外网历史告警数据获取异常", e);
		}
		
		try {
			list=this.getAlarmHistory(OperatorEnums.Operator_yd_hlw, drd_yd_hlw, list);
		} catch (Exception e) {
			log.error(OperatorEnums.Operator_yd_hlw.getName()+"获取数据异常>>>>> {}", ProdUtils.exception(e));
			exception=new NetrustException("移动互联网历史告警数据获取异常", e);
		}
		
		//组装
		List<Server> slist= serverDao.findAll();
		
		if (CollectionUtils.isNotEmpty(list)) {
			List finalList=new AlarmConverUtils().originHistory(list,slist);
			//保存
			historyAlarmDao.save(finalList);
		}
		
		//保存最后读取的行值
//		dateReadRecordDao.save(Arrays.asList(drd_dx_zwww,drd_dx_hlw,drd_yd_zwww,drd_yd_hlw));	
		
		if(exception!=null){
			throw exception;
		}
	}
	
	
	
	private List getAlarmHistory(OperatorEnums oe,DateReadRecord drr,List list) throws Exception{
		try {
			CommonHttp ch=new CommonHttp();
			
			oc.setMo(oe.getName());
			int drr_size=StringUtils.isNotBlank(drr.getValue())?Integer.parseInt(drr.getValue()):0;
			
			List<String> strList=ch.getAlarmHistory(oc,drr_size);
			
			if (CollectionUtils.isNotEmpty(strList)) {
				drr_size+=strList.size();
				drr.setValue(String.valueOf(drr_size));

				dateReadRecordDao.save(drr);
			
				List ahlist=ProdUtils.pushList(strList);
				
				if(CollectionUtils.isNotEmpty(ahlist)){
					list.addAll(ahlist);
				}
			}
			return list;
		} catch (Exception e) {
			throw new NetrustException(e);
		}
	}
	
	
}
